Couchbase Forums has a question Can’t use N1QL on docker-compose. This blog will show how to run Couchbase using Docker Compose and run a N1QL query.
What is Docker Compose?
Docker Compose allows you to define your multi-container application with all of its dependencies in a single file, then spin your application up in a single command.
Docker Compose introduced v3 in Docker 1.13. How do you know what version of Docker are you running?
docker version
command gives you that information:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Client: Version: 1.13.1 API version: 1.26 Go version: go1.7.5 Git commit: 092cba3 Built: Wed Feb 8 08:47:51 2017 OS/Arch: darwin/amd64 Server: Version: 1.13.1 API version: 1.26 (minimum version 1.12) Go version: go1.7.5 Git commit: 092cba3 Built: Wed Feb 8 08:47:51 2017 OS/Arch: linux/amd64 Experimental: true |
Couchbase Docker Compose File
Now if you see this version of Docker, then you can use the following Compose file:
1 2 3 4 5 6 7 8 9 10 11 12 |
version: "3" services: db: image: arungupta/couchbase deploy: replicas: 1 ports: - 8091:8091 - 8092:8092 - 8093:8093 - 8094:8094 - 11210:11210 |
In this Compose file:
- v3 version of Compose file. If you are using an older version of Docker, then you can consider using v2 version of Compose file.
arungupta/couchbase
Docker image is used to start Couchbase server. This image is created as explained at github.com/arun-gupta/docker-images/tree/master/couchbase. It uses Couchbase REST API to pre-configure the Couchbase server.- Ports 8091, 8092, 8093, 8094, 11210 are exposed.
- Only a single replica of Couchbase server is started.
Couchbase can be started in a couple of ways using this Compose file.
Couchbase using Docker Compose on Single Docker Host
If you want to start Couchbase on a single host (such as provisioned by Docker for Mac or a single Docker Machine), then use the command:
1 |
docker-compose up -d |
This will show the warning message but starts Couchbase server:
1 2 |
WARNING: Some services (db) use the 'deploy' key, which will be ignored. Compose does not support deploy configuration - use `docker stack deploy` to deploy to a swarm. Creating couchbase_db_1 |
Check the status of started service using the command docker-compose ps
:
1 2 3 4 5 6 7 8 9 10 |
Name Command State Ports ----------------------------------------------------------------------------------------------------------------- couchbase_db_1 /entrypoint.sh /opt/couchb Up 11207/tcp, ... 0.0.0.0:11210->11210/tcp, 11211/tcp, 18091/tcp, 18092/tcp, 18093/tcp, 0.0.0.0:8091->8091/tcp, 0.0.0.0:8092->8092/tcp, 0.0.0.0:8093->8093/tcp, 0.0.0.0:8094->8094/tcp |
All the exposed ports are shown and Couchbase is accessible at http://localhost:8091. Use the credentials Administrator/password to access the web console.
Now you can create buckets and connect from CBQ and run N1QL queries. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
/Users/arungupta/tools/couchbase/Couchbase\ Server\ 4.5\ EE.app/Contents/Resources/couchbase-core/bin/cbq -u Administrator -p password --engine http://localhost:8093 Connected to : http://localhost:8093/. Type Ctrl-D or \QUIT to exit. Path to history file for the shell : /Users/arungupta/.cbq_history cbq> select now_str(); { "requestID": "d28280ab-49a4-4254-9f00-06bd1d2b4695", "signature": { "$1": "string" }, "results": [ { "$1": "2017-02-13T21:36:57.248Z" } ], "status": "success", "metrics": { "elapsedTime": "2.916653ms", "executionTime": "2.829056ms", "resultCount": 1, "resultSize": 56 } } cbq> select version(); { "requestID": "51091fa6-dcc5-40f6-9c2b-1eb6732630bb", "signature": { "$1": "string" }, "results": [ { "$1": "1.6.0" } ], "status": "success", "metrics": { "elapsedTime": "4.599365ms", "executionTime": "4.525552ms", "resultCount": 1, "resultSize": 37 } } |
Typically, you may be able to scale the services started by Docker Compose using docker-compose scale
command. But this will not be possible in our case as the ports are exposed. Scaling a service will cause port conflict.
The container can be brought down using the command docker-compose down
.
Couchbase using Docker Compose on Multi-host Swarm-mode Cluster
Docker allows multiple hosts to be configured in a cluster using Swarm-mode. This can be configured using the command docker swarm init
.
Once the cluster is initialized, then the Compose file can be used to start the cluster:
1 |
docker deploy --compose-file=docker-compose.yml couchbase |
It shows the output:
1 2 |
Creating network couchbase_default Creating service couchbase_db |
This creates a Docker service and the status can be seen using the command docker service ls
:
1 2 |
ID NAME MODE REPLICAS IMAGE 0zls1k4mgrry couchbase_db replicated 1/1 arungupta/couchbase:latest |
Check the tasks/containers running inside the service using the command docker service ps couchbase_db
:
1 2 |
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS vf5zicu4mhei couchbase_db.1 arungupta/couchbase:latest moby Running Running 3 hours ago |
Here again, you can connect to the Couchbase server and run N1QL queries:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
/Users/arungupta/tools/couchbase/Couchbase\ Server\ 4.5\ EE.app/Contents/Resources/couchbase-core/bin/cbq -u Administrator -p password --engine http://localhost:8093 Connected to : http://localhost:8093/. Type Ctrl-D or \QUIT to exit. Path to history file for the shell : /Users/arungupta/.cbq_history cbq> select version(); { "requestID": "12c5581e-44ee-4ea7-9017-6a017bb60a58", "signature": { "$1": "string" }, "results": [ { "$1": "1.6.0" } ], "status": "success", "metrics": { "elapsedTime": "3.725498ms", "executionTime": "3.678153ms", "resultCount": 1, "resultSize": 37 } } cbq> select now_str(); { "requestID": "efe034fa-6d00-4327-9fc9-da8f6d15d95c", "signature": { "$1": "string" }, "results": [ { "$1": "2017-02-13T21:38:33.502Z" } ], "status": "success", "metrics": { "elapsedTime": "853.491µs", "executionTime": "800.154µs", "resultCount": 1, "resultSize": 56 } } |
The service, and thus the container running in the service, can be terminated using the command docker service couchbase_db
.
Any more questions? Catch us on Couchbase Forums.
You may also consider running Couchbase Cluster using Docker or read more about Deploying Docker Services to Swarm.
Want to learn more about running Couchbase in containers?